package org.sanm.domain.goods;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import org.sanm.common.Result;
import org.sanm.domain.category.Category;
import org.sanm.domain.company.Company;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Override
    public Goods getDetail(Integer id) {
        Goods goods = getById(id);
        Company company = Db.getById(goods.getCompanyId(), Company.class);
        goods.setCompany(company);
        Category category = Db.getById(goods.getCategoryId(), Category.class);
        goods.setCategory(category);
        return goods;
    }

    @Override
    public List<Goods> getByIds(List<Integer> ids) {
        List<Goods> list = lambdaQuery().in(Goods::getId, ids).list();

        if(list.isEmpty()){
            return list;
        }

        List<Integer> companyIds = list.stream().map(Goods::getCompanyId).collect(Collectors.toList());
        List<Company> companyList = Db.lambdaQuery(Company.class).in(Company::getId, companyIds).list();
        Map<Integer, Company> collect = companyList.stream().collect(Collectors.toMap(Company::getId, c -> c));

        List<Integer> categoryIds = list.stream().map(Goods::getCategoryId).toList();
        List<Category> categoryList = Db.lambdaQuery(Category.class).in(Category::getId, categoryIds).list();
        Map<Integer, Category> categoryMap = categoryList.stream().collect(Collectors.toMap(Category::getId, item -> item));

        for(Goods g : list){
            Company company = collect.get(g.getCompanyId());
            g.setCompany(company);
            Category category = categoryMap.get(g.getCategoryId());
            g.setCategory(category);
        }
        return list;
    }

    @Override
    public Page<Goods> page(Integer pageNum, Integer pageSize, String name, Integer companyId, Integer categoryId) {
        Page<Goods> page = Page.of(pageNum, pageSize);
        lambdaQuery()
                .like(name!= null, Goods::getName, name)
                .eq(companyId != null, Goods::getCompanyId, companyId)
                .eq(categoryId != null, Goods::getCategoryId, categoryId)
                .orderByDesc(Goods::getCreateTime)
                .page(page);

        if(page.getRecords().isEmpty()){
            return page;
        }

        List<Goods> goodsList = page.getRecords();
        List<Integer> companyIds = goodsList.stream().map(Goods::getCompanyId).collect(Collectors.toList());
        List<Company> companyList = Db.lambdaQuery(Company.class).in(Company::getId, companyIds).list();
        Map<Integer, Company> collect = companyList.stream().collect(Collectors.toMap(Company::getId, c -> c));

        List<Integer> categoryIds = goodsList.stream().map(Goods::getCategoryId).toList();
        List<Category> categoryList = Db.lambdaQuery(Category.class).in(Category::getId, categoryIds).list();
        Map<Integer, Category> categoryMap = categoryList.stream().collect(Collectors.toMap(Category::getId, item -> item));

        for(Goods g : goodsList){
            Company company = collect.get(g.getCompanyId());
            g.setCompany(company);
            Category category = categoryMap.get(g.getCategoryId());
            g.setCategory(category);
        }

        return page;
    }
}
